//
//  HUD.m
//  GameProject
//
//  Created by Blair Trusler on 25/07/2011.
//  Copyright 2011 Blair Trusler. All rights reserved.
//

//Heads-Up-Display (HUD) for game. Sits above background layer. Provides D-Pad and action/menu buttons.

#import "GameHeader.h"

@implementation HUD


#pragma mark - buttons
-(void) initDPad{
    CCLOG(@"Loading D-Pad...");
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    CGRect dPadBaseSize;
    CGPoint dPadBasePos;
    SneakyJoystickSkinnedBase *dPadBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease]; 
    
    dPadBase.backgroundSprite =  [CCSprite spriteWithFile:@"d_pad.png"];  
    dPadBase.thumbSprite = [CCSprite spriteWithFile:@"red_button.png"];        
    dPadBase.thumbSprite.scale = 0.5f;

    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        CCLOG(@"Scaling HUD to iPad");
        dPadBaseSize = CGRectMake (0, 0, 256.0f, 256.0f); //sets D-Pad sprite size
        dPadBasePos = ccp(100, 100);
    }
    else {
        CCLOG(@"Scaling HUD to iPhone");
        dPadBase.thumbSprite.scale = 0.4f;
        dPadBase.backgroundSprite.scale = 0.75f;
        dPadBaseSize = CGRectMake (0, 0, 64.0f, 64.0f); //sets D-Pad sprite size
        dPadBasePos = ccp(screenSize.width*0.15f,screenSize.height*0.25f);
    }
    
    dPadBase.position = dPadBasePos;                  

    dPadBase.joystick = [[SneakyJoystick alloc] initWithRect:dPadBaseSize]; 
    
    dPad = [dPadBase.joystick retain];  
    
    dPadBase.backgroundSprite.opacity = 200;
    dPadBase.thumbSprite.opacity = 200;

    [self addChild:dPadBase];         
    
    CCLOG(@"D-Pad Loaded.");
}


//Initialises stat menu icon at top right of screen
//eventually include all HUD buttons in this: 
-(void) initButtons{
    CCLOG(@"Loading Buttons...");
    CGSize screenSize = [[CCDirector sharedDirector] winSize]; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        //Stat Menu Button
        CGPoint statIconPos = ccp(975,720);
        CGRect statIconDimensions = CGRectMake(0, 0, 64.0f, 64.0f);
        SneakyButtonSkinnedBase *statIcon = [[[SneakyButtonSkinnedBase alloc] init] autorelease];    
        statIcon.scale = 0.3f;
        statIcon.position = statIconPos;
        statIcon.defaultSprite = [CCSprite spriteWithFile:@"pausebutton1.png"];   //change image 
        //  statIcon.activatedSprite = [CCSprite spriteWithFile:@"jumpUp.png"];     
        //  statIcon.pressSprite = [CCSprite spriteWithFile:@"jumpUp.png"];        
        statIcon.button = [[SneakyButton alloc]  initWithRect:statIconDimensions]; 
        statButton = [statIcon.button retain];    
        statButton.isToggleable = NO;  
        statIcon.defaultSprite.opacity = 200;
        [self addChild:statIcon];
        
        boostMenuItem = [CCMenuItemImage itemFromNormalImage:@"green_button.png" selectedImage:@"green_button.png" target:self selector:@selector(boostAction:)];
        boostMenuItem.scale = 0.75f;
        boostMenuItem.position = ccp(950, 75);
        boostMenu = [CCMenu menuWithItems:boostMenuItem, nil];
        boostMenu.position = CGPointZero;
        [self addChild:boostMenu];
        
        NSString *timeCounterLabelString = [NSString stringWithFormat:@""];
        timerCounterLabel = [CCLabelTTF labelWithString:timeCounterLabelString fontName:@"Courier" fontSize:24];
        timerCounterLabel.position = ccp(950,75);
        [self addChild: timerCounterLabel];
        
        boostNotificationString = [NSString stringWithFormat:@""];
        boostNotification = [CCLabelTTF labelWithString:boostNotificationString fontName:@"Courier" fontSize:32];
        boostNotification.position = ccp(500,650);
        [self addChild: boostNotification];

        
    }
    else{
        //Stat Menu Button
        CGPoint statIconPos = ccp(445,290);
        CGRect statIconDimensions = CGRectMake(0, 0, 64.0f, 64.0f);
        SneakyButtonSkinnedBase *statIcon = [[[SneakyButtonSkinnedBase alloc] init] autorelease];        
        statIcon.defaultSprite = [CCSprite spriteWithFile:@"pausebutton1.png"];   
        statIcon.defaultSprite.scale = 0.2f;
        statIcon.position = statIconPos;
        statIcon.button = [[SneakyButton alloc]  initWithRect:statIconDimensions]; 
        statButton = [statIcon.button retain];    
        statButton.isToggleable = NO;  
        statIcon.defaultSprite.opacity = 200;
        [self addChild:statIcon];
        
        boostMenuItem = [CCMenuItemImage itemFromNormalImage:@"green_button.png" selectedImage:@"green_button.png" target:self selector:@selector(boostAction:)];
        boostMenuItem.scale = 0.65f;
        boostMenuItem.position = ccp(screenSize.width*0.9,screenSize.height*0.15);
        boostMenu = [CCMenu menuWithItems:boostMenuItem, nil];
        boostMenu.position = CGPointZero;
        [self addChild:boostMenu];
        
        NSString *timeCounterLabelString = [NSString stringWithFormat:@""];
        timerCounterLabel = [CCLabelTTF labelWithString:timeCounterLabelString fontName:@"Courier" fontSize:24];
        timerCounterLabel.position = ccp(screenSize.width*0.9,screenSize.height*0.15);
        [self addChild: timerCounterLabel];
        
        boostNotificationString = [NSString stringWithFormat:@""];
        boostNotification = [CCLabelTTF labelWithString:boostNotificationString fontName:@"Courier" fontSize:32];
        boostNotification.position = ccp(screenSize.width*0.5,screenSize.height*0.6);
        [self addChild: boostNotification];
    }
    CCLOG(@"Buttons Loaded.");
}

- (void)timerForButton:(ccTime)dt {    
    boostTicker += dt;    
    int boostResetTime = boostButtonTimerReset-boostTicker;
    NSString *timeCounterLabelString;
    
    if (boostResetTime!=0){
        boostNotificationString=@"";
        timeCounterLabelString = [NSString stringWithFormat:@"%d",boostResetTime];
    }
    else{
        timeCounterLabelString = [NSString stringWithFormat:@""];
    }
    [timerCounterLabel setString:[NSString stringWithFormat:timeCounterLabelString]];
//    boostNotificationString = [NSString stringWithFormat:@"SPRINTING"];
//    [boostNotification runAction:[CCSequence actions: [CCFadeOut actionWithDuration:1.0f],nil]];

    if (boostTicker ==  3){
        CCLOG(@"RETURNING PLAYER SPEED TO NORMAL");
        boostNotificationString = [NSString stringWithFormat:@"SPRINT OVER"];
        [boostNotification runAction:[CCSequence actions: [CCFadeOut actionWithDuration:1.0f],nil]];
        [AudioManager powerUpOver];
//        [self unschedule:@selector(timerForButton:)];
//        boostTicker = 0;
        speedBoost=0;
        speedBoostTilt=0;
    }
    if (boostTicker == boostButtonTimerReset){      //boostButtonTimerReset == 20 seconds
        CCLOG(@"RE-ENABLING BOOST BUTTON");
        boostNotificationString = [NSString stringWithFormat:@"SPRINT READY"];
        [boostNotification runAction:[CCSequence actions: [CCFadeIn actionWithDuration:1.0f],[CCFadeOut actionWithDuration:1.0f],nil]];
        [self unschedule:@selector(timerForButton:)];
        boostTicker = 0;
        boostMenuItem.isEnabled = YES;
    }
    [boostNotification setString:[NSString stringWithFormat:boostNotificationString]];
}

-(void)boostAction:(id)sender{
    CCLOG(@"BOOSTING PLAYER SPEED");
    boostNotificationString = [NSString stringWithFormat:@"SPRINTING!"];
    [boostNotification runAction:[CCSequence actions: [CCFadeOut actionWithDuration:1.5f],nil]];
    [boostNotification setString:[NSString stringWithFormat:boostNotificationString]];
    [AudioManager powerUp];
    boostMenuItem.isEnabled = NO;
    speedBoost+=120;
    speedBoostTilt += 2;
    [self schedule:@selector(timerForButton:) interval:1.0f];
}



//- (void)timerForButton:(ccTime)dt {
//    [self unschedule:@selector(timerForButton:)];
//    button.isEnabled = YES;
//}


-(void)applyButtons {
    if ( statButton.value == 0 ) freeToJump = YES;
    if ( statButton.value == 1 && freeToJump ) {
        freeToJump = NO;
        if (statButton.active == YES) { 
            CCLOG(@"Pause Menu Button Pressed.");
            [AudioManager playMenuButtonClick];
            
                //REPLACE WITH SCENE MANAGER?            
            //pauses game
            [[CCDirector sharedDirector] pause];

            CCScene *loadStatScene = [StatMenuScene node];
            [[CCDirector sharedDirector] pushScene: loadStatScene]; //'pushes' stat scene ontop of game scene
        }
    }    
    if ( boostButton.value == 0 ) freeToJump = YES;
    if ( boostButton.value == 1 && freeToJump ) {
        freeToJump = NO;
        if (boostButton.active == YES) { 
            CCLOG(@"Sprint Button Pressed.");
//            [self boostAction];
            //Increase scaledVelocity of player sprite for X seconds
            speedBoost+=50;
            
            //Add boost timer, and number of times you can press it.
            
        }
    }
}


-(void)createObjectOfType:(GameObjectType)objectType atLocation:(CGPoint)spawnLocation  withZValue:(int)ZValue {
    if (objectType == kEnemyType1) {
        CCLOG(@"Creating Enemy NPC (type 1)");
        //Make CCSprite *enemy; then assign the tag, instead of Global Sprites? Investigate :) 
        enemy1 = [[EnemySprite alloc] initWithSpriteFrameName:@"E2Down1.png"];
        [enemy1 setPosition:spawnLocation];
        [sceneSpriteBatchNode addChild:enemy1 z:ZValue tag:kEnemySpriteTagValue];
        enemy1.scale = 1.5f;
        [enemy1 release];
    } 
    if (objectType == kEnemyType2){
        CCLOG(@"Creating Enemy NPC (type 2)");
        enemy2 = [[EnemySprite2 alloc] initWithSpriteFrameName:@"E1Down1.png"];
        [enemy2 setPosition:spawnLocation];
        [sceneSpriteBatchNode addChild:enemy2 z:ZValue tag:kEnemySpriteTagValue];
        [enemy2 release];
    }
    if (objectType == kEnemyType3){
        CCLOG(@"Creating Enemy NPC (type 3)");
        enemy3 = [[EnemySprite2 alloc] initWithSpriteFrameName:@"E1Down1.png"];
        [enemy3 setPosition:spawnLocation];
        [sceneSpriteBatchNode addChild:enemy3 z:ZValue tag:kEnemySpriteTagValue];
        [enemy3 release];

    }
    if (objectType == kEnemyType4){
        CCLOG(@"Creating Enemy NPC (type 4)");
        enemy4 = [[EnemySprite2 alloc] initWithSpriteFrameName:@"E1Down1.png"];
        [enemy4 setPosition:spawnLocation];
        [sceneSpriteBatchNode addChild:enemy4 z:ZValue tag:kEnemySpriteTagValue];
        [enemy4 release];
        
    }
    if (objectType == kEnemyType5){
        CCLOG(@"Creating Enemy NPC (type 5)");
        enemy5 = [[EnemySprite2 alloc] initWithSpriteFrameName:@"E1Down1.png"];
        [enemy5 setPosition:spawnLocation];
        [sceneSpriteBatchNode addChild:enemy5 z:ZValue tag:kEnemySpriteTagValue];
        [enemy5 release];
        
    }
    
}



-(void)createPlayerObject{
    playerX = [[PlayerSprite alloc] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"Down1.png"]];
    [playerX setPosition:kPlayerSpawnLocation]; 
    [playerX setJoystick:dPad];
    [sceneSpriteBatchNode addChild:playerX z:kPlayerSpriteZValue tag:kPlayerSpriteTagValue];
}


-(void)displayInfo{
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    
    CCLabelTTF *playerLevel;
    if (levelPlayed==1){
        playerLevel = [CCLabelTTF labelWithString:@"Level 1" fontName:@"Courier" fontSize:24];
    }
    if (levelPlayed == 2){
        playerLevel = [CCLabelTTF labelWithString:@"Level 2" fontName:@"Courier" fontSize:24];
    }
    if (levelPlayed == 3){
        playerLevel = [CCLabelTTF labelWithString:@"Level 3" fontName:@"Courier" fontSize:24];
    }
    if (levelPlayed == 4){
        playerLevel = [CCLabelTTF labelWithString:@"Level 4" fontName:@"Courier" fontSize:24];
    }
    if (levelPlayed == 5){
        playerLevel = [CCLabelTTF labelWithString:@"Level 5" fontName:@"Courier" fontSize:24];
    }

    timerNumberLabel = [CCLabelTTF labelWithString:@"Timer: 0:00" fontName:@"Courier" fontSize:24];
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        playerLevel.position =  ccp(54,750);
        timerNumberLabel.position =  ccp(500,750);
    }
    else{
        //ccp(screenSize.width*0.0947f,screenSize.height*0.169f);
        playerLevel.position =  ccp(screenSize.width*0.1,screenSize.height*0.95f);//ccp(54,750);
        timerNumberLabel.position = ccp(screenSize.width*0.5,screenSize.height*0.95f); //ccp(500,750);
    }
    
    [self addChild:playerLevel];
    [self addChild:timerNumberLabel];
}

//Increments timer by 1 every second:
-(void) tick: (ccTime) dt{
    timeSeconds++;
    NSString *timerNumber;
    if (timeSeconds<10){
        timerNumber = [NSString stringWithFormat:@"Timer: %d:0%d",timeMinutes,timeSeconds];
    }
    else{
        //If one minute is completed, increment minutes by 1 and reset seconds to 0
        if (timeSeconds>=60){  
            timeSeconds=0;
            timeMinutes++;
            timerNumber = [NSString stringWithFormat:@"Timer: %d:0%d",timeMinutes,timeSeconds];
        }
        else{
            timerNumber = [NSString stringWithFormat:@"Timer: %d:%d",timeMinutes,timeSeconds]; 
        }
    }
    [timerNumberLabel setString:[NSString stringWithFormat:timerNumber]];
}

-(void)spawnObjects{
    if (levelPlayed == 1){
        [self createObjectOfType: kEnemyType1 atLocation:kEnemySpawnLocation1 withZValue:kEnemySpriteZValue];        
        [self createPlayerObject];
    }
    
    else if (levelPlayed == 2){
        [self createObjectOfType: kEnemyType1 atLocation:kEnemySpawnLocation1 withZValue:kEnemySpriteZValue];  
        [self createObjectOfType: kEnemyType2 atLocation:kEnemySpawnLocation2 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType3 atLocation:kEnemySpawnLocation3 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType4 atLocation:kEnemySpawnLocation4 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType5 atLocation:kEnemySpawnLocation5 withZValue:kEnemySpriteZValue];   
        [self createPlayerObject];
    }
    else if (levelPlayed == 3){
        [self createObjectOfType: kEnemyType1 atLocation:kEnemySpawnLocation1 withZValue:kEnemySpriteZValue];  
        [self createObjectOfType: kEnemyType2 atLocation:kEnemySpawnLocation2 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType3 atLocation:kEnemySpawnLocation3 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType4 atLocation:kEnemySpawnLocation4 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType5 atLocation:kEnemySpawnLocation5 withZValue:kEnemySpriteZValue];   
        [self createPlayerObject];
    }
    else if (levelPlayed == 4){
        [self createObjectOfType: kEnemyType1 atLocation:kEnemySpawnLocation1 withZValue:kEnemySpriteZValue];  
        [self createObjectOfType: kEnemyType2 atLocation:kEnemySpawnLocation2 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType3 atLocation:kEnemySpawnLocation3 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType4 atLocation:kEnemySpawnLocation4 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType5 atLocation:kEnemySpawnLocation5 withZValue:kEnemySpriteZValue];   
        [self createPlayerObject];
    }
    else if (levelPlayed == 5){
        [self createObjectOfType: kEnemyType1 atLocation:kEnemySpawnLocation1 withZValue:kEnemySpriteZValue];  
        [self createObjectOfType: kEnemyType2 atLocation:kEnemySpawnLocation2 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType3 atLocation:kEnemySpawnLocation3 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType4 atLocation:kEnemySpawnLocation4 withZValue:kEnemySpriteZValue];   
        [self createObjectOfType: kEnemyType5 atLocation:kEnemySpawnLocation5 withZValue:kEnemySpriteZValue];   
        [self createPlayerObject];
        
        
    }

}

#pragma mark - initialize
-(id) init
{
    if ((self = [super init])) {
        CCLOG(@"Loading HUD...");
        //Enable touch interaction:
        self.isTouchEnabled = YES;
        
        boostButtonTimerReset = 20;
        boostTicker = 0;
        boostNotificationString = @"";
        
        //Initialize HUD components:
        [self initButtons];
        [self displayInfo];
        
        if (dPadOn == TRUE){
            [self initDPad];      
        }

        //Create game sprites:
        [self spawnObjects];

        //Initialize timer:
        timeSeconds = 0;
        timeMinutes = 0;
        
        //Schedule timer: 
        [self schedule: @selector(tick:)interval: 1];

        //Schedule update method:
        [self scheduleUpdate]; 
        
        CCLOG(@"HUD Loaded.");
    }
    return self;
}


#pragma mark - dealloc
-(void) dealloc {

    
    [super dealloc];
}


#pragma mark - 
#pragma mark update
-(void) update: (ccTime)deltaTime{
    //Apply buttons so that they are constantly checking for user input:
    [self applyButtons];    
    
    CCArray *listOfGameObjects = [sceneSpriteBatchNode children];
    
    for (GameCharacter *tempChar in listOfGameObjects){
        [tempChar updateStateWithDeltaTime:deltaTime andListOfGameObjects:listOfGameObjects];
    }
}

@end
